//
//  BaseTabBarController.m
//  VirtualCurrency
//
//  Created by apple on 2021/7/5.
//

#import "BaseTabBarController.h"
#import "VCHomeController.h"
#import "VCMillController.h"
#import "VCInformationController.h"
#import "VCNewsController.h"
#import "VCMineController.h"
#import "BaseNavigationController.h"

@interface BaseTabBarController ()<UITabBarControllerDelegate>
@property (nonatomic,assign) NSInteger index;

@end

@implementation BaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createTabBar];

    // Do any additional setup after loading the view.
}

- (void)createTabBar{
    self.tabBar.tintColor = Main_Blue;
    [self createControllerWithTitle:k_Language(@"首页") image:@"home_icon"selectedimage:@"home_icon_sel" className:[VCHomeController class]];
    [self createControllerWithTitle:k_Language(@"矿机") image:@"market_icon"selectedimage:@"market_icon_sel" className:[VCMillController class]];
    [self createControllerWithTitle:k_Language(@"资讯") image:@"assets_icon"selectedimage:@"assets_icon_sel" className:[VCInformationController class]];
    [self createControllerWithTitle:k_Language(@"消息") image:@"assets_icon"selectedimage:@"assets_icon_sel" className:[VCNewsController class]];
    [self createControllerWithTitle:k_Language(@"我的") image:@"my_icon"selectedimage:@"my_icon_sel" className:[VCMineController class]];
    

}
//提取公共方法
- (void)createControllerWithTitle:(NSString *)title image:(NSString *)image selectedimage:(NSString *)selectedimage  className:(Class)class{
    UIViewController *vc = [[class alloc]init];
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
    nav.tabBarItem.title = title;
    
    nav.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:selectedimage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self addChildViewController:nav];
    nav.tabBarController.tabBar.backgroundColor = [UIColor whiteColor];
//    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:RGB(57, 104, 217), NSForegroundColorAttributeName,
//                                                       nil,nil] forState:UIControlStateSelected];
}

// 点击tabbarItem自动调用
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    
    NSInteger index = [self.tabBar.items indexOfObject:item];
    if (index==_index) {
        _index=index;
        return;
    }
    
    if (index != _index) {
       [self animationWithIndex:index];
        _index = index;
    }
    
}
- (void)animationWithIndex:(NSInteger) index {
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    return;

    /**
     CABasicAnimation类的使用方式就是基本的关键帧动画。
     
     所谓关键帧动画，就是将Layer的属性作为KeyPath来注册，指定动画的起始帧和结束帧，然后自动计算和实现中间的过渡动画的一种动画方式。
     */
    /*CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.2;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    [[tabbarbuttonArray[index] layer] addAnimation:pulse forKey:nil];*/
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
